<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="utf-8">
    <title>权限管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/lib/layui-v2.5.5/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all">

    <span th:replace="common_page::common"></span>
</head>
<body>

<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" id="functionSearchForm" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">所属应用</label>
                            <div class="layui-input-inline">
                                <select id="selectApp" name="appCode"  lay-search="">
                                    <option value="">请选择</option>
                                    <option th:each="aa : ${apps}" th:value="${aa.code}" th:text="${aa.code}+' '+${aa.name}">
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                            <button type="reset" class="layui-btn layui-btn-primary" id="data-reset-btn" > 重 置</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <span th:each="tb : ${toolbarButtons}" th:utext="${tb}"></span>
            </div>
        </script>

        <table class="layui-hide" id="orgnazitionTreeTable" lay-filter="orgnazitionTreeTableFilter"></table>

        <!-- 操作列 -->
        <script type="text/html" id="orgnazitionTreeTableToolbar">
            <span th:each="btn : ${rowButtons}" th:utext="${btn}"></span>
        </script>

    </div>
</div>
<script th:src="@{/lib/layui-v2.5.5/layui.js}" charset="utf-8"></script>
<script th:src="@{/js/lay-config.js?v=1.0.4}" charset="utf-8"></script>
<script th:src="@{/js/common/selectApp.js}" charset="utf-8"></script>

<script>

    layui.use(['form', 'table', 'treetable'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;
        var treetable = layui.treetable;

        $.fn.serializeJson=function(){
            var serializeObj={};
            var array=this.serializeArray();
            var str=this.serialize();
            $(array).each(function(){
                if(serializeObj[this.name]){
                    if($.isArray(serializeObj[this.name])){
                        serializeObj[this.name].push(this.value);
                    }else{
                        serializeObj[this.name]=[serializeObj[this.name],this.value];
                    }
                }else{
                    serializeObj[this.name]=this.value;
                }
            });
            return serializeObj;
        };

        function getFormParam()
        {
            var formParam = $("#functionSearchForm").serializeJson();
            return formParam;
        }

        $("#data-reset-btn").click(function(){
            $("#functionSearchForm")[0].reset();
            layui.form.render();
        });


        function loadTreeTable()
        {
            treetable.render({
                treeColIndex: 1,
                treeSpid: -1,
                treeIdName: 'id',
                treePidName: 'pid',
                elem: '#orgnazitionTreeTable',
                where:getFormParam(),
                url: '[[@{/}]]orgnazition/tree/table',
                toolbar: '#toolbarDemo',
                page: false,
                cols: [
                    [
                        {type: "checkbox", width: 50},
                        {field: 'name', width: 400,  title: '组织机构名称'},
                        {field: 'appNames', title: '应用名称'},
                        {field: 'orgnazitionSort', title: '排序'},
                        {field: 'createAdminUsername', title: '创建人'},
                        {field: 'createDate',  title: '创建时间'},
                        {field: 'updateAdminUsername',  title: '修改人'},
                        {field: 'updateDate',  title: '修改时间'},
                        {templet: '#orgnazitionTreeTableToolbar', width: 120, align: 'center', title: '操作'}
                    ]
                ],
                done: function () {
                    treetable.foldAll("#orgnazitionTreeTable");
                    layer.closeAll('loading');
                }
            });

        }

        loadTreeTable();

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {

            loadTreeTable();

            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(orgnazitionTreeTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加组织机构',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,

                    area: ['100%', '100%'],
                    content: "[[@{/orgnazition/addPage}]]",
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('orgnazitionTreeTable')
                    , data = checkStatus.data;
                if(data==null||data.length<=0)
                {
                    layer.msg("请选择要操作的记录");
                    return;
                }
                layer.confirm('确定删除组织机构?', function (index) {
                    $.ajax({
                        url:"[[@{/}]]orgnazition/delete/ids",
                        contentType: "application/json; charset=utf-8",
                        type:'DELETE',
                        data:JSON.stringify(data),
                        success:function(data){
                            layer.msg(data.msg);

                            //执行搜索重载
                            loadTreeTable();

                        },
                        complete:function(data)
                        {
                            layer.close(index);
                        }
                    });
                });
            }
        });

        //监听表格复选框选择
        table.on('checkbox(orgnazitionTreeTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(orgnazitionTreeTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '编辑组织机构',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,

                    area: ['100%', '100%'],
                    content: '[[@{/}]]orgnazition/editPage/'+data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确定删除该组织机构?', function (index) {
                    $.ajax({
                        url:"[[@{/}]]orgnazition/delete/"+data.id,
                        contentType: "application/json; charset=utf-8",
                        type:'DELETE',
                        success:function(data){
                            layer.msg(data.msg);

                            //执行搜索重载
                            loadTreeTable();

                        },
                        complete:function(data)
                        {
                            layer.close(index);
                        }
                    });
                });
            }
        });

    });
</script>

</body>
</html>